package com.example.backgroundtask;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


public class ThreadLifeCycleActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = "ThreadLifeCycleActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_threadlifecycle);
        findViewById(R.id.ac_threadlifecycle_priority_btn).setOnClickListener(this);
        findViewById(R.id.ac_threadlifecycle_daemon_btn).setOnClickListener(this);
        findViewById(R.id.ac_threadlifecycle_sleep_btn).setOnClickListener(this);
        findViewById(R.id.ac_threadlifecycle_yield_btn).setOnClickListener(this);
        findViewById(R.id.ac_threadlifecycle_join_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ac_threadlifecycle_priority_btn:
                goPriorityThread();
                break;
            case R.id.ac_threadlifecycle_daemon_btn:
                Toast.makeText(this,"请在Java项目中运行DaemonTest.java",Toast.LENGTH_SHORT).show();
                break;
            case R.id.ac_threadlifecycle_sleep_btn:
                //
                break;
            case R.id.ac_threadlifecycle_yield_btn:
                //
                goYieldThread();
                break;
            case R.id.ac_threadlifecycle_join_btn:
                //
                goJoinThread();
                break;
        }
    }

    //----------------------线程合并Start----------------------------//
    private void goJoinThread(){
        JoinRunnable m = new JoinRunnable();

        Thread t1 = new Thread(m,"Join t1");
        Thread t2 = new Thread(m,"Join t2");

        t1.start();
        t2.start();

        //这里是main主线程的循环
        for (int i = 1; i < 10; i++) {
            Log.i(TAG,"主线程执："+i);
            if (i == 5){
                try {
                    //当i==5时，在main主线程中调用t1、t2的join方法，让main主线程处于等待阻塞状态，
                    // 等t1、t2执行完了，main主线程才有执行的机会
                    t1.join();
                    t2.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    class JoinRunnable implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                Log.i(TAG,Thread.currentThread().getName()+":"+i);
            }
        }
    }
    //----------------------线程合并End----------------------------//

    //----------------------线程让步Start----------------------------//
    private void goYieldThread(){
        YieldRunnable yieldRunnable = new YieldRunnable();

        Thread t1 = new Thread(yieldRunnable,"Yield t1");
        Thread t2 = new Thread(yieldRunnable,"Yield t2");
        //设置线程优先级
        t1.setPriority(10);
        t2.setPriority(1);

        t2.start();
        t1.start();

        for (int i = 0; i < 100; i++) {
            Log.i(TAG,"主线程执行了："+i+"次");
        }

    }

    class YieldRunnable implements Runnable{

        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                if (i % 5 == 0){
                    Thread.yield();//run方法中调用yeild方法
                }
                Log.i(TAG,Thread.currentThread().getName()+":"+i);
            }
        }
    }
    //----------------------线程让步End----------------------------//


    //----------------------线程优先级Start----------------------------//
    private void goPriorityThread(){
        PriorityRunnable priorityRunnable = new PriorityRunnable();

        Thread t1 = new Thread(priorityRunnable,"Priority t1");
        Thread t2 = new Thread(priorityRunnable,"Priority t2");

        //给两个不同的线程设置一个最高优先级，一个最低优先级
        t1.setPriority(Thread.MAX_PRIORITY);
        t2.setPriority(Thread.MIN_PRIORITY);
        //t1.setPriority(9);也可以使用数字取值范围为1~~10

        t2.start();
        t1.start();

    }
    class PriorityRunnable implements Runnable{


        @Override
        public void run() {
            test();
        }

        private  void test() {
            for (int i = 0; i < 1000; i++) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.i(TAG,Thread.currentThread().getName()+":"+i);
            }
        }
    }
    //----------------------线程优先级End----------------------------//


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}